package com.ruoyi.web.controller.market.form;

import java.util.List;
import java.util.Map;

import com.ruoyi.db.domain.FormInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.FormInfoItem;
import com.ruoyi.form.service.IFormInfoItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.wapper.FormInfoItemWapper;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 表单推送子项Controller
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Controller
@RequestMapping("/form/item")
public class FormInfoItemController extends BaseController
{
    private String prefix = "form/item";

    @Autowired
    private IFormInfoItemService formInfoItemService;

//    @RequiresPermissions("form:item:view")
    @GetMapping("/{formId}")
    public String item(@PathVariable("formId") Long formId, ModelMap mmap)
    {
        mmap.put("formId", formId);
        return prefix + "/item";
    }

    /**
     * 查询表单推送子项列表
     */
//    @RequiresPermissions("form:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormInfoItem formInfoItem)
    {
        startPage();
        List<Map<String, Object>> mapList = formInfoItemService.selectFormInfoItemListMap(formInfoItem);
        mapList = new FormInfoItemWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出表单推送子项列表
     */
    @RequiresPermissions("form:item:export")
    @Log(title = "表单推送子项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FormInfoItem formInfoItem)
    {
        List<FormInfoItem> list = formInfoItemService.selectFormInfoItemList(formInfoItem);
        ExcelUtil<FormInfoItem> util = new ExcelUtil<FormInfoItem>(FormInfoItem.class);
        return util.exportExcel(list, "表单推送子项数据");
    }

    /**
     * 新增表单推送子项
     */
    @GetMapping("/add/{formId}")
    public String add(@PathVariable("formId") String formId, ModelMap mmap)
    {
        mmap.put("formId", formId);
        return prefix + "/add";
    }

    /**
     * 新增保存表单推送子项
     */
//    @RequiresPermissions("form:item:add")
    @Log(title = "表单推送子项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormInfoItem formInfoItem)
    {
        return toAjax(formInfoItemService.insertFormInfoItem(formInfoItem));
    }

    /**
     * 修改表单推送子项
     */
//    @RequiresPermissions("form:item:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FormInfoItem formInfoItem = formInfoItemService.selectFormInfoItemById(id);
        mmap.put("formInfoItem", formInfoItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存表单推送子项
     */
//    @RequiresPermissions("form:item:edit")
    @Log(title = "表单推送子项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormInfoItem formInfoItem)
    {
        return toAjax(formInfoItemService.updateFormInfoItem(formInfoItem));
    }

    /**
     * 删除表单推送子项
     */
    @RequiresPermissions("form:item:remove")
    @Log(title = "表单推送子项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(formInfoItemService.deleteFormInfoItemByIds(ids));
    }

//    @RequiresPermissions("form:info:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(FormInfoItem formInfoItem)
    {
        return toAjax(formInfoItemService.updateFormInfoItem(formInfoItem));
    }


}
